package com.bls.blslib.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.bls.blslib.eventbus.Event;
import com.bls.blslib.eventbus.EventBusUtils;
import com.bls.blslib.view.LoadingDogDialog;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2017/12/23.
 */

public abstract class BaseFragment extends BaseRootFragment implements BaseView {

    protected Gson gson;
    protected BasePresenter presenter;
    protected LoadingDogDialog loadingDogDialog;

    protected Activity activity;
    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected String mParam1;
    protected String mParam2;

    @Override
    protected void initOnAttach(Context context) {
        this.activity = (Activity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        if (isRegisterEventBus()) {
            EventBusUtils.register(this);
        }
        presenter = new BasePresenter(this);
        loadingDogDialog = new LoadingDogDialog(activity);
        gson = new Gson();
    }

    @Override
    protected void initRoot() {

    }

    @Override
    protected void initParams() {

    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected void initOnHiddenChanged(boolean hidden) {

    }

    @Override
    protected void initOnDestroyView() {
    }

    @Override
    protected void initOnDestroy() {
        if (null != presenter) presenter.onDestroy();

        if (isRegisterEventBus()) EventBusUtils.register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(Event event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(Event event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(Event event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(Event event) {

    }

    @Override
    public void showOnStart(Request request, int requestId) {
        loadingDogDialog.show();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {

    }

    @Override
    public void showOnCacheSuccess(int requestId, String responseBody, Response response) {

    }

    @Override
    public void showOnError(int requestId, String responseBody, Response response) {

    }

    @Override
    public void showOnFinish(int requestId) {
        loadingDogDialog.dismiss();
    }

}
